﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCP.UI.Blocks;
using Baosight.ZNH.ZMCP.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;

namespace Baosight.ZNH.ZMCP.UI
{
    public partial class ZMCPB3 : EFViewCmes
    {
        private ProduceProblemBlock _block = new ProduceProblemBlock();
        public ZMCPB3()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(_block.getProblemOrigin());
            initData.AddBlock(_block.getProblemStatus());
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击反馈按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnFeedbackButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup(eiInfo, "生产问题-反馈", "feedback", null, sender, e);
        }

        /// <summary>
        /// 弹出新增修改页面
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            AddProduceProblem _problem = new AddProduceProblem(_dialogType);//构造一个新的修改页面

            _eIInfo.AddBlock(_block.getProblemOrigin());//传递问题来源block
            _eIInfo.AddBlock(_block.getIsCheck());//是否核查block
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.problemType"));//问题类型 
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.problemLevel"));//问题级别 
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.department"));//部门

            if (_dialogType.Equals("feedback"))
            {
                if ("是".Equals(_eIInfo.GetBlock(EIConstants.ResultBlock).GetRow(0)["fdIsAudit"]))
                {
                    _eIInfo.GetBlock(EIConstants.ResultBlock).GetRow(0)["fdProblemStatus"] = "3";
                }
                else
                {
                    _problem.fdAuditDepartmentId.Visibility = Visibility.Collapsed;
                    _eIInfo.GetBlock(EIConstants.ResultBlock).GetRow(0)["fdProblemStatus"] = "4";
                }
            }

            _problem.EIDataContext = new Platform.Core.UI.EIDataContext();
            _problem.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _problem;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }


        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "生产问题-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 页签切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {
            EFTab tab = sender as EFTab;
            if (tab.SelectedItem != null)
            {
                EFTabItem item = tab.SelectedItem as EFTabItem;
                if (tab.SelectedItem.Equals(this.tab2))
                {
                    item.Content = null;
                    this.tab2.LoadView("ZMCPB4");
                }

            }
        }
    }
}
